/**
Company: Sarkar software technologys
WebSite: http://www.sarkar.cn
Author: yeganaaa
Date : 5/25/18
Time: 2:19 PM
 */

package Cn.Sarkar.MMS.WebAPI

import Cn.Sarkar.Entities.MMS.ToJsonEntities.JApplication
import Cn.Sarkar.Entities.MMS.ToJsonEntities.JCompany
import Cn.Sarkar.EntityDream.CoreEngine.RDBMS.Core.QueryBuilderExtensions.SelectQueryExpression.equals
import Cn.Sarkar.EntityDream.CoreEngine.RDBMS.Core.QueryBuilderExtensions.SelectQueryExpression.singleOrNull
import Cn.Sarkar.MMS.DataBaseDefinations.Additional.Countries
import Cn.Sarkar.MMS.Entities.Additional.Country
import Cn.Sarkar.MMS.Entities.Additional.Description
import Cn.Sarkar.MMS.Entities.Additional.Name
import Cn.Sarkar.MMS.Entities.DataSources.IDataSource
import Cn.Sarkar.MMS.LayerOne.EntityBuilder
import Cn.Sarkar.MMS.LayerOne.convertToDbEntity
import Cn.Sarkar.ResponseModels.success
import io.ktor.locations.Location
import io.ktor.locations.location
import io.ktor.request.receive
import io.ktor.routing.Route
import io.ktor.routing.put


fun Route.useApplicationController(){
    @Location("Application")
    class ApplicationController

    location<ApplicationController> {
        put("Initialize") {
            success {
                IDataSource.openAndAutoClose { db ->
                    if (db.applications.size > 0) return@openAndAutoClose

                    val receivedApplication = context.receive<JApplication>()
                    var correct = false

                    receivedApplication.company.name.run {
                        correct = false
                        correct = correct or ug.isNotEmpty()
                        correct = correct or zh.isNotEmpty()
                        correct = correct or en.isNotEmpty()
                        correct = correct or kz.isNotEmpty()
                        correct = correct or ru.isNotEmpty()
                        if (!correct) throw IllegalArgumentException("Company names cannot be empty.")
                    }
                    receivedApplication.company.description.run {
                        correct = false
                        correct = correct or ug.isNotEmpty()
                        correct = correct or zh.isNotEmpty()
                        correct = correct or en.isNotEmpty()
                        correct = correct or kz.isNotEmpty()
                        correct = correct or ru.isNotEmpty()
                        if (!correct) throw IllegalArgumentException("Company descriptions cannot be empty.")
                    }
                    receivedApplication.company.contacts.run {
                        correct = true
                        correct = correct and phoneNumber.isNotEmpty()
                        correct = correct and eMail.isNotEmpty()
                        correct = correct and (qqNumber.isNotEmpty() or weChatNumber.isNotEmpty())

                        if (!correct) throw IllegalArgumentException("Company contacts cannot be empty.")
                    }

                    var receivedCountry: Country? = null

                    receivedApplication.company.addr.run {
                        correct = true
                        receivedCountry = db.countries.singleOrNull { Countries.ID equals id }
                        correct = correct and (receivedCountry != null)
                        if (!correct) throw IllegalArgumentException("Company Address cannot be empty.")
                    }


                    val app = EntityBuilder.makeApplication(db).apply {

                    }

                    db.applications.add(app)
                    db.saveChanges()
//                    val domain = EntityBuilder.makeApplicationDomain(db)
//                    db.applicationDomains.add(domain)
//                    db.saveChanges()
                }
            }
        }
    }
}
